package com.qf.service.impl;

import com.qf.entity.User;
import com.qf.mapper.UserMapper;
import com.qf.service.UserListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

/**
 * --- 今日无事,勾栏听曲 ---
 *
 * @author JinZhiHao
 * @date 2024/12/2
 * @desc
 */

@Service
public class UserListServiceImpl implements UserListService {


    @Autowired
    private UserMapper userMapper;
    @Override
    public List<User> list(User user) {
        return userMapper.list(user);
    }

    @Override
    public void add(User user) {


        // 获取添加人名称
        // 以下代码需要使用Springmvc,即控制层也需要由Spring托管,才会获得ip
        HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
        User user1 = (User) request.getSession().getAttribute("user");
        // User user1 = (User) request.getSession().getAttribute("user");

        if (user1 != null){

            String userName = user1.getUsername();

            // 设置创建人,创建时间,更新人,更新时间
            user.setCreateBy(userName);
            user.setCreateTime(new Date());
            user.setUpdateBy(userName);
            user.setUpdateTime(new Date());
        }



        userMapper.add(user);
    }

    @Override
    public void update(User user) {
        userMapper.update(user);
    }

    @Override
    public int del(Integer id) {
        return userMapper.del(id);
    }
}
